function ajax(t, msg, data) {
    if (t.val().length > 0) {
        msg.html('<img src="images/ajax-loader.gif" width="16" />');
        $.ajax({
            url: 'AjaxServlet',
            data: data,
            success: function(r) {
                msg.html(r);
            }
        });
    } else {
        msg.empty();
    }
}

$.tools.validator.fn('[data-equals]', 'Not Same Password', function(input) {
    var name = input.attr('data-equals'),
    field = this.getInputs().filter('[name=' + name + ']');
    return input.val() == field.val() ? true : [name];
});

$.tools.validator.fn('[minlength]', function(input, value) {
    var min = input.attr('minlength');
    return value.length >= min ? true : {
        en: 'Too Short Password'
    };
});

$.widget('custom.catcomplete', $.ui.autocomplete, {
    _renderMenu: function(ul, items) {
        var self = this,
        currentCategory = '';
        $.each(items, function(index, item) {
            if (item.category != currentCategory) {
                ul.append('<li class="ui-autocomplete-category">' + item.category + '</li>');
                currentCategory = item.category;
            }
            self._renderItem(ul, item);
        });
    }
});

$.editable.addInputType('date', {
    element : function() {
        var input = $('<input type="date" />');
        $(this).append(input);
        return(input);
    },
    plugin : function() {
        var form = this;
        $('input', this).dateinput({
            selectors: true,
            yearRange: [-100,-5],
            format: 'mm/dd/yyyy'
        });
    }
});

$.editable.addInputType('email', {
    element : function() {
        var input = $('<input type="email" class="text" />');
        $(this).append(input);
        return(input);
    },
    plugin : function() {
        var form = this;
        form.validator({
            lang: 'en'
        });
    }
});

var addressFormatting = function(text){
    var newText = text;
    var findreps = [
    {
        find:/^([^\-]+) \- /g,
        rep: '<span class="ui-selectmenu-item-header">$1</span>'
    },

    {
        find:/([^\|><]+) \| /g,
        rep: '<span class="ui-selectmenu-item-content">$1</span>'
    },

    {
        find:/([^\|><\(\)]+) (\()/g,
        rep: '<span class="ui-selectmenu-item-content">$1</span>$2'
    },

    {
        find:/([^\|><\(\)]+)$/g,
        rep: '<span class="ui-selectmenu-item-content">$1</span>'
    },

    {
        find:/(\([^\|><]+\))$/g,
        rep: '<span class="ui-selectmenu-item-footer">$1</span>'
    }
    ];

    for(var i in findreps){
        newText = newText.replace(findreps[i].find, findreps[i].rep);
    }
    return newText;
}